#include <iostream>
using namespace std;

class Solution
{
public:
    string reverseOnlyLetters(string s)
    {
        int begin = 0;
        int end = s.size() - 1;
        while (begin < end)
        {
            while (begin < end && !isalpha(s[begin]))
            {
                begin++;
            }
            while (begin < end && !isalpha(s[end]))
            {
                end--;
            }
            // 交换字母
            if (begin < end)
            {
                swap(s[begin], s[end]);
                begin++;
                end--;
            }
        }
        return s;
    }
};